// pages/addressList/index.js
import { addressList, addressAdd, addressRemove, addressEdit, addressDefaultId, addressSetDefault, regionTree } from '../../service/index.js'

Page({
  /**
   * 页面的初始数据 
   */
  data: {
    address: [],
    defaultAddressId: '',
    updateAddressId: '',
    insertName: '',
    updateName: '',
    insertPhone: '',
    updatePhone: '',
    insertDetail: '',
    updateDetail: '',
    insertRegionStr: '',
    insertRegion: [],
    updateRegionStr: '',
    updateRegion: '',
    pageType: 1,//1:列表 2:新增 3:编辑
    areaList: [],
    codeMap: {},
    showAreaPicker: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.addressList()
    // console.log(this.data.areaList);
    regionTree({}, (res) => {
      this.setData({
        areaList: this.transformData(res.data)
      })
    })
  },
  transformData: function (jsonData) {
    let provinceList = {};
    let cityList = {};
    let countyList = {};
    const codeMapData = this.data.codeMap

    // 遍历省级数据
    for (let key in jsonData.list) {
      let province = jsonData.list[key];
      if (province.level === 1) {
        provinceList[province.id * 10000] = province.name;
        codeMapData[province.id * 10000] = province.id
        // 遍历市级数据
        for (let cityKey in province.city) {
          let city = province.city[cityKey];
          cityList[province.id * 10000 + city.id * 100] = city.name;
          codeMapData[province.id * 10000 + city.id * 100] = city.id
          // 遍历区县级数据
          for (let regionKey in city.region) {
            let region = city.region[regionKey];
            countyList[province.id * 10000 + city.id * 100 + region.id] = region.name;
            codeMapData[province.id * 10000 + city.id * 100 + region.id] = region.id
          }
        }
      }
      this.setData({
        codeMap: codeMapData
      })
    }
    const areaList = {
      province_list: provinceList,
      city_list: cityList,
      county_list: countyList
    }
    console.log(areaList);
    return areaList
  },
  onChange(event) {
    this.setData({
      checked: event.detail
    });
  },
  insertAddressPage(event) {
    this.setData({
      pageType: 2,
      insertName: '',
      insertPhone: '',
      insertDetail: '',
      insertRegion: []
    });
  },
  updateAddressPage(event) {
    const address = event.currentTarget.dataset.address
    this.setData({
      pageType: 3,
      updateAddressId: address.address_id,
      updateName: address.name,
      updatePhone: address.phone,
      updateDetail: address.detail,
      updateRegion: JSON.stringify([
        {
          "label": address.region.province,
          "value": address.province_id
        },
        {
          "label": address.region.city,
          "value": address.city_id
        },
        {
          "label": address.region.region,
          "value": address.region_id
        }
      ]),
      updateRegionStr: address.region.province + address.region.city + address.region.region
    });
  },
  addressSetDefault(event) {
    const addressId = event.currentTarget.dataset.id
    // console.log(addressId, event.detail, 111)
    addressSetDefault({
      addressId: addressId,
    }, (res) => {
      wx.showToast({ title: res.message, icon: 'success' })
      this.addressList()
    })
  },
  addressList(event) {
    addressDefaultId({}, (res) => {
      this.setData({
        defaultAddressId: res.data.defaultId,
      })
      addressList({}, (res) => {
        res.data.list.map(value => {
          if (value.address_id === this.data.defaultAddressId) {
            value.checked = true
          }
          return value
        })
        this.setData({
          address: res.data.list,
          pageType: 1
        })
        console.log(res, this.data.address)
      })
    })
  },
  addressInsert(event) {
    addressAdd({
      form: {
        name: this.data.insertName,
        phone: this.data.insertPhone,
        region: JSON.parse(this.data.insertRegion),
        detail: this.data.insertDetail,
      }
    }, (res) => {
      wx.showToast({ title: res.message, icon: 'success' })
      this.addressList()
    })
  },
  addressUpdate(event) {
    addressEdit({
      addressId: String(this.data.updateAddressId),
      form: {
        name: this.data.updateName,
        phone: this.data.updatePhone,
        region: JSON.parse(this.data.updateRegion),
        detail: this.data.updateDetail,
      }
    }, (res) => {
      wx.showToast({ title: res.message, icon: 'success' })
      this.addressList()
    })
  },

  addressRemove(event) {
    addressRemove({
      addressId: event.currentTarget.dataset.addressid
    }, (res) => {
      this.addressList()
      wx.showToast({ title: res.message, icon: 'success' })
    })
  },
  onFocus() {
    this.setData({
      showAreaPicker: true
    });
  },
  onAreaConfirm(event) {
    const { values } = event.detail;
    const [province, city, county] = values;
    this.setData({
      insertRegion: JSON.stringify([
        {
          "label": province.name,
          "value": this.data.codeMap[province.code]
        },
        {
          "label": city.name,
          "value": this.data.codeMap[city.code]
        },
        {
          "label": county.name,
          "value": this.data.codeMap[county.code]
        }
      ]),
      showAreaPicker: false,
      insertRegionStr: province.name + city.name + county.name
    });
  },

  onAreaConfirm2(event) {
    const { values } = event.detail;
    const [province, city, county] = values;
    this.setData({
      updateRegion: JSON.stringify([
        {
          "label": province.name,
          "value": this.data.codeMap[province.code]
        },
        {
          "label": city.name,
          "value": this.data.codeMap[city.code]
        },
        {
          "label": county.name,
          "value": this.data.codeMap[county.code]
        }
      ]),
      showAreaPicker: false,
      updateRegionStr: province.name + city.name + county.name
    });
  },
  onAreaCancel() {
    this.setData({
      showAreaPicker: false
    });
  }
})
